//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
// --------------------------------------------------------------
//   GEANT 4 - Underground Dark Matter Detector Advanced Example
//
//      For information related to this code contact: Alex Howard
//      e-mail: alexander.howard@cern.ch
// --------------------------------------------------------------
// Comments
//
//                  Underground Advanced
//               by A. Howard and H. Araujo 
//                    (27th November 2001)
//
// History:
// 21 Feb 2002 AH: Added Analysis
//
// SteppingAction program
// --------------------------------------------------------------

#include "DMXSteppingAction.hh"
#include "DMXSteppingActionMessenger.hh"

#include "DMXEventAction.hh"
#include "DMXRunAction.hh"

#ifdef G4ANALYSIS_USE
#include "DMXAnalysisManager.hh"
#endif

#include "G4Track.hh"
#include "G4Step.hh"
#include "G4StepPoint.hh"
#include "G4VProcess.hh"
#include "G4TrackStatus.hh"
#include "G4ParticleDefinition.hh"
#include "G4ParticleTypes.hh"
#include "G4VVisManager.hh"
#include "G4Colour.hh"
#include "G4Polyline.hh" 
#include "G4VisAttributes.hh"
#include "globals.hh"
#include "G4ios.hh"

//calculate em crosssection
#include "G4EmCalculator.hh"
#include "G4Material.hh"
#include "G4NistManager.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

DMXSteppingAction::DMXSteppingAction(DMXRunAction* run,DMXEventAction* eventAction)
  : Run(run),evtAction(eventAction)   
{

  steppingMessenger = new DMXSteppingActionMessenger(this);

  // defaults for messenger
  colourNeutronFlag      = "magenta";
  colourGammaFlag        = "cyan";
  colourOpticalFlag      = "white";
  colourChargedPlusFlag  = "red";
  colourChargedMinusFlag = "blue";

  colourMuFlag = "white";

}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

DMXSteppingAction::~DMXSteppingAction()
{

  delete steppingMessenger;

}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....

void DMXSteppingAction::UserSteppingAction(const G4Step* fStep)
{

  // removed 28/11/01 - unnecessary unless program "freezes"
  // kill track if too many steps
  // NB: This is set to DBL_MAX - therefore may cause program to "hang"
  //  G4int MaxNoSteps = DBL_MAX;
  //  G4int StepNo = fStep->GetTrack()->GetCurrentStepNumber();
  //  if(StepNo >= MaxNoSteps) fStep->GetTrack()->SetTrackStatus(fStopAndKill);

  //--"StepNo" will start from 1
  G4int   StepNo = fStep->GetTrack()->GetCurrentStepNumber();
  G4int  TrackID = fStep->GetTrack()->GetTrackID();
  G4int ParentID = fStep->GetTrack()->GetParentID();
  G4String pname = fStep->GetTrack()->GetDefinition()->GetParticleName();
  G4String procname = fStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName();
  G4String namev = fStep->GetPostStepPoint()->GetPhysicalVolume()->GetName();
  G4String namev2= fStep->GetPreStepPoint()->GetPhysicalVolume()->GetName();
  G4float edep   = fStep->GetTotalEnergyDeposit();
  G4double stepl = fStep->GetStepLength();
  
  if( 1==TrackID ){
    if( namev2=="pLAr" ){
      evtAction->AddTrackdedl( edep, fStep->GetStepLength() );
    }
  }

  /*************************************/
  G4float xpos = fStep->GetPostStepPoint()->GetPosition().x();
  G4float ypos = fStep->GetPostStepPoint()->GetPosition().y();
  G4float zpos = fStep->GetPostStepPoint()->GetPosition().z();
  G4float xpre = fStep->GetPreStepPoint()->GetPosition().x();
  G4float ypre = fStep->GetPreStepPoint()->GetPosition().y();
  G4float zpre = fStep->GetPreStepPoint()->GetPosition().z();
  G4float px = fStep->GetPreStepPoint()->GetMomentum().x();
  G4float py = fStep->GetPreStepPoint()->GetMomentum().y();
  G4float pz = fStep->GetPreStepPoint()->GetMomentum().z();
  G4float time = fStep->GetPreStepPoint()->GetGlobalTime();
  G4float ekin = fStep->GetTrack()->GetKineticEnergy();

  Particle particletmp;
  particletmp.particlename = pname;
  particletmp.processname = procname;
  particletmp.volumename = namev;
  particletmp.volumename2 = namev2;
  particletmp.TrackID  = TrackID;
  particletmp.ParentID = ParentID;
  particletmp.xpre = xpre;
  particletmp.ypre = ypre;
  particletmp.zpre = zpre;
  particletmp.xpos = xpos;
  particletmp.ypos = ypos;
  particletmp.zpos = zpos;
  particletmp.vpx = px;
  particletmp.vpy = py;
  particletmp.vpz = pz;
  particletmp.edep = edep;
  particletmp.time = time;
  particletmp.ekin = ekin;

  if( pname=="alpha"){
    evtAction->FillAlpha( particletmp );
    //    if(edep*50000.>10) G4cout << "Alpha Number of Photon = " << edep*50000. << G4endl;
  }
  if( pname=="neutron"){
    evtAction->FillNeutron( particletmp );
  }
  if( pname=="Ar40[0.0]"){
    evtAction->FillArgon( particletmp );
  }
  if( pname=="gamma"){
    if( StepNo==1 ) evtAction->CountGamma();
    evtAction->FillGamma( particletmp );
  }
  if( pname=="e-"){
    //    if( StepNo==1 ) evtAction->Addcounter(6);
    evtAction->FillElectron( particletmp );

    //    if(edep*50000.>10) G4cout << "Electron Number of Photon = " << edep*50000. << G4endl;

    //    if( StepNo==1 ){
      // G4EmCalculator* emCalculator = new G4EmCalculator();
      // G4NistManager* nist = G4NistManager::Instance();
      // const G4ParticleDefinition* particle = fStep->GetTrack()->GetDefinition();
      // const G4String procnameconst = procname; 
      // const G4Material* material = nist->FindOrBuildMaterial("G4_lAr");
      // G4double density = material->GetDensity();
      // G4double cut = 0.0;
      // G4double massSigma = emCalculator->ComputeCrossSectionPerVolume((G4double)ekin,particle,procnameconst,material,cut)/density;
      // G4cout << StepNo << G4endl;
      // G4cout << namev2 << G4endl;
      // G4cout << procnameconst << G4endl;
      // G4cout << G4BestUnit(massSigma, "Surface/Mass") << G4endl;
      //    }

  }
  if(pname=="e+"){
    //    evtAction->AddEdep(edep);
    evtAction->FillElectron( particletmp );
  }
  if (pname=="opticalphoton"){
      PMT pmttmp;
      pmttmp.xpos = xpos;
      pmttmp.ypos = ypos;
      pmttmp.zpos = zpos;
      pmttmp.time = time;
      pmttmp.ekin = ekin;

      evtAction->FillClassParticle( particletmp );

      // fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
      // fStep->GetTrack()->SetTrackStatus(fStopAndKill);

      if( StepNo==1 && ekin > 5.0e-6) {
	evtAction->Addcounter(0);
      }
      if (namev=="pJPMT1" && ekin < 5.0e-6)  {
	evtAction->Addcounter(1);
      }
      if (namev=="pJPMT2" && ekin < 5.0e-6)  {
	evtAction->Addcounter(2);
      }
      if (namev=="pJPMT3" && ekin < 5.0e-6)  {
	evtAction->Addcounter(13);
      }
      if (namev=="pJPMT4" && ekin < 5.0e-6)  {
	evtAction->Addcounter(14);
      }
      if (namev=="pJPMT5" && ekin < 5.0e-6)  {
	evtAction->Addcounter(15);
      }
      if (namev=="pJPMT6" && ekin < 5.0e-6)  {
	evtAction->Addcounter(16);
      }
      if (namev=="pMPMT1" && ekin < 5.0e-6)  {
	evtAction->Addcounter(3);
	//	G4cout << "pMPMT1 = " << xpos << G4endl;
      }
      if (namev=="pMPMT2" && ekin < 5.0e-6)  {
	evtAction->Addcounter(4);
	//	G4cout << "pMPMT2 = " << xpos << G4endl;
      }
      if (namev=="pvetoPMT1" && ekin < 5.0e-6)  {
	evtAction->Addcounter(5);
      }
      if (namev=="pvetoPMT2" && ekin < 5.0e-6)  {
	evtAction->Addcounter(6);
      }
      if (namev=="pvetoPMT3" && ekin < 5.0e-6)  {
	evtAction->Addcounter(7);
      }
      if (namev=="pvetoPMT4" && ekin < 5.0e-6)  {
	evtAction->Addcounter(8);
      }
      if (namev=="pvetoPMT5" && ekin < 5.0e-6)  {
	evtAction->Addcounter(9);
      }
      if (namev=="pvetoPMT6" && ekin < 5.0e-6)  {
	evtAction->Addcounter(10);
      }
      if (namev=="pvetoPMT7" && ekin < 5.0e-6)  {
	evtAction->Addcounter(11);
      }
      if (namev=="pvetoPMT8" && ekin < 5.0e-6)  {
	evtAction->Addcounter(12);
      }

  }

  /***************************************************/

  if(StepNo == 1) 
    { 
      G4double partEnergy = fStep->GetPreStepPoint()->GetKineticEnergy();
      G4ParticleDefinition* particleType = fStep->GetTrack()->GetDefinition();
      G4String particleName = particleType->GetParticleName();
      //DMXAnalysisManager* analysis =  DMXAnalysisManager::getInstance();
      //analysis->analyseParticleSource(partEnergy, particleName);
    }
  //#endif
  // check what is to be drawn from EventAction/EventActionMessenger
  G4String drawColsFlag = evtAction->GetDrawColsFlag();
  G4String drawTrksFlag = evtAction->GetDrawTrksFlag();

  // draw by step (here) instead of by event (event action)
  if (drawColsFlag=="custom" && drawTrksFlag!="none") {

    // check that VisManager exists
    G4VVisManager* pVVisManager = G4VVisManager::GetConcreteInstance();
    if(pVVisManager) {

      // particle colour in a string
      G4String name = fStep->GetTrack()->GetDefinition()->GetParticleName();
      G4String strColour;
      if(name=="neutron") {
	if(drawTrksFlag=="charged") return;
	strColour = colourNeutronFlag;
      } else if (name=="gamma") {
	if(drawTrksFlag=="charged") return;
	strColour = colourGammaFlag;
      } else if (name=="opticalphoton") {
	if(drawTrksFlag!="all") return;
	strColour = colourOpticalFlag;
      }else if (name=="mu+"||name=="mu-"){
	strColour = colourMuFlag;
      }
      else if (name=="alpha" || name=="e+")
	strColour = colourChargedPlusFlag;
      else
	strColour = colourChargedMinusFlag;

      // convert string to G4Colour
      G4Colour colour;
      if     (strColour=="white")    colour=G4Colour(1.0, 1.0, 1.0);
      else if(strColour=="grey" )    colour=G4Colour(0.5, 0.5, 0.5);
      else if(strColour=="lgrey")    colour=G4Colour(.75, .75, .75);
      else if(strColour=="black")    colour=G4Colour(0.0, 0.0, 0.0);
      else if(strColour=="red")      colour=G4Colour(1.0, 0.0, 0.0);
      else if(strColour=="green")    colour=G4Colour(0.0, 1.0, 0.0);
      else if(strColour=="blue")     colour=G4Colour(0.0, 0.0, 1.0);
      else if(strColour=="cyan")     colour=G4Colour(0.0, 1.0, 1.0);
      else if(strColour=="magenta")  colour=G4Colour(1.0, 0.0, 1.0);
      else if(strColour=="yellow")   colour=G4Colour(1.0, 1.0, 0.0);
      else if(strColour=="lgreen")   colour=G4Colour(0.0, .75, 0.0);
      else if(strColour=="lblue")    colour=G4Colour(0.0, 0.0, .75);
      else                           colour=G4Colour(1.0, 1.0, 1.0);

      // create line with colour
      G4VisAttributes attribs(colour);
      G4Polyline polyline;
      polyline.SetVisAttributes(attribs);

      // draw line
      G4Point3D start(fStep->GetPreStepPoint()->GetPosition());
      G4Point3D end(fStep->GetPostStepPoint()->GetPosition());
      polyline.push_back(start);
      polyline.push_back(end);
      pVVisManager->Draw(polyline);
    }
    
  }

  if(pname!="opticalphoton"){

    if(namev=="world_phys") fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //  if(namev=="pLab") fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);

    if(namev=="world_phys") fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    //  if(namev=="pLab") fStep->GetTrack()->SetTrackStatus(fStopAndKill);

  }

  //************Only collimeter Run****************//

  // if(pname="gamma"){
  //   if (namev=="pPb1"||namev=="pPb2")  {
  //     fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
  //     fStep->GetTrack()->SetTrackStatus(fStopAndKill);
  //   }
  // }

  //************Only Polished Polished****************//

  if(pname=="opticalphoton"){

    //    /*
    //if (namev2=="pFid" && (namev=="prefwlsS"||namev=="prefwlsTt"||namev=="prefwlsTb") && stepl==0)  {
    //  fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //  fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    //}

    //    fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //    fStep->GetTrack()->SetTrackStatus(fStopAndKill);

    // if (namev2=="pMFid" && namev=="pMrefwls" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

    // if (namev2=="pMPMTw1" && namev=="pMPwls1" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

    // if (namev2=="pMPMTw2" && namev=="pMPwls2" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }    //    */

    // if (namev2=="pJFid" && namev=="pJrefwls" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

    // if (namev2=="pJPMTw1" && namev=="pJPwls1" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

    // if (namev2=="pJPMTw2" && namev=="pJPwls2" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }    //    */

    // if (namev2=="pJPMTw3" && namev=="pJPwls3" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

    // if (namev2=="pJPMTw4" && namev=="pJPwls4" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }    //    */

    // if (namev2=="pJPMTw5" && namev=="pJPwls5" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

    // if (namev2=="pJPMTw6" && namev=="pJPwls6" && stepl==0)  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }    //    */

    // if (namev=="pMrefwls" || namev=="pMPwls1" ||namev=="pMPwls2")  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

    // if (namev=="pMPwls1" ||namev=="pMPwls2")  {
    //   fStep->GetTrack()->SetTrackStatus(fKillTrackAndSecondaries);
    //   fStep->GetTrack()->SetTrackStatus(fStopAndKill);
    // }

  }

}
